// pages/huifu/huifu.js
const app = getApp();
var router = require("../../router.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.setData({
        id: options.id
      });     
  },
  changeInputData: function (e) {
    var vlaue = e.detail.value;
    this.setData({
      content: vlaue
    });
  },
  sub: function () {
    var that = this;
    var content = that.data.content;
    var id = that.data.id;  
    if (content && id) {
      app.request({
        url: router.msg.comAdd,
        method: "POST",
        data: {
          uid: wx.getStorageSync('uid'),
          content: content,
          id: id,         
        },
        success: function (data) {
          console.log(data);  
          if (data) {
            wx.showToast({
              title: '发布成功',
              icon: 'none'
            });
            setTimeout(function () {
              app.navTo('../liuyan/liuyan?id=' + id)             
            }, 1500);
          } else {
            wx.showToast({
              title: '发布失败,请重试',
              icon: 'none'
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: '请填写正确的评论信息',
        duration: 2000,
        icon: 'none'
      })
    }
  },
  //分享
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      var that = this;
      // 来自页面内转发按钮    
      var id = res.target.dataset.id;
      var index = res.target.dataset.ctr;
      var title = that.data.list[index].name;
      return {
        title: title,
        path: '/pages/huifu/huifu',
        success: function (res) {
          app.request({
            url: router.msg.share,
            method: "POST",
            data: {
              id: id,
            },
            success: function (data) {
              if (data) {
                var list = that.data.list;
                var share = list[index].share;
                list[index].share = share + 1;
                that.setData({
                  list: list
                })
                wx.showToast({
                  title: '分享成功',
                  icon: 'success',
                  duration: 2000
                });
              } else {
                wx.showToast({
                  title: '分享失败请重试',
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          });
        },
        fail: function (res) {
          wx.showToast({
            title: '分享已取消',
            icon: 'none',
            duration: 2000
          });
        }
      }
    }
  },
})